package com.supermap.business.cloud.stat;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.redis.JedisComponent;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.stat.StatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author aqua
 * @description 新版APP统计
 * @date 2019年11月28 14:33
 * @email 1301584033@qq.com
 */
@Controller
@RequestMapping("/cloud/stat")
public class StatController {
    @Reference(timeout = 100000)
    private StatService statService;
    @Autowired
    JedisComponent jedisComponent;
    //功能点击
    @GetMapping("/getMenuClickCountList")
    @ResponseBody
    public JSONUtils getMenuClickCountList(String areaCode,Integer limit){
        String key="/cloud/stat/getMenuGroupByCountList"+areaCode+limit;
        JSONUtils jsonUtils=null;
        if(jedisComponent.exists(key)){
            jsonUtils=new JSONUtils().setData(jedisComponent.getObject(key));
            return  jsonUtils;
        }else{
            jsonUtils=statService.getMenuClickCountList(areaCode,limit);
        }
        //设置缓存
        jedisComponent.setObject(key,jsonUtils.getData());
        //设置时效 保存10分钟
        jedisComponent.setKeyTime(key,600);
        //设置时效 保存10分钟
        return jsonUtils;
    }
    //菜单条数统计
    @GetMapping("/getMenuReleaseCountList")
    @ResponseBody
    public JSONUtils getMenuReleaseCountList(String areaCode,String btime,String etime){
        String key="/cloud/stat/getMenuGroupByCountList"+areaCode+etime+btime;
        JSONUtils jsonUtils=null;
        if(jedisComponent.exists(key)){
            jsonUtils=new JSONUtils().setData(jedisComponent.getObject(key));
            return  jsonUtils;
        }else{
            jsonUtils=statService.getMenuReleaseCountList(areaCode,btime,etime);
        }
        //设置缓存
        jedisComponent.setObject(key,jsonUtils.getData());
        //设置时效 保存半个小时
        jedisComponent.setKeyTime(key,600);
        //设置时效 保存半个小时
        return jsonUtils;
    }
    //分组统计
    @GetMapping("/getMenuGroupByCountList")
    @ResponseBody
    public JSONUtils getMenuGroupByCountList(String areaCode,String btime,String etime,Integer state){
        String key="/cloud/stat/getMenuGroupByCountList"+state+areaCode+etime+btime+state;
        JSONUtils jsonUtils=null;
        if(jedisComponent.exists(key)){
            jsonUtils=new JSONUtils().setData(jedisComponent.getObject(key));
            return  jsonUtils;
        }else{
            jsonUtils=statService.getMenuGroupByCountList(areaCode,btime,etime,state);
        }
            jedisComponent.setObject(key,jsonUtils.getData());
            jedisComponent.setKeyTime(key,600);
            return jsonUtils;
        }

    @GetMapping("/getFarmingList")
    @ResponseBody
    public JSONUtils getFarmingList(String areaCode,Integer userId,Integer farmId,Integer page,Integer size){
        String key="getFarmingList"+areaCode+userId+farmId+page+size;
        JSONUtils jsonUtils=null;
        if(jedisComponent.exists(key)){
            jsonUtils=new JSONUtils().setData(jedisComponent.getObject(key));
            return  jsonUtils;
        }else{
            jsonUtils=statService.getFarmingList(areaCode,userId,farmId,page,size);
        }
        jedisComponent.setObject(key,jsonUtils.getData());
        jedisComponent.setKeyTime(key,1800);
        return jsonUtils;
    }
}
